package com.qingcheng.controller;

import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Spec;
import com.qingcheng.pojo.goods.TbSpec;
import com.qingcheng.service.SpecService;
import com.qingcheng.service.TbSpecService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
/**
 *
 * @author Simon
 * @date 2020/07/27
 */
@Slf4j
@RestController
@RequestMapping("/spec")
@Api(tags = "TbSpecController")
public class SpecController {

    @Autowired
    private SpecService specService;

    @Resource
    private TbSpecService tbSpecService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {
        TbSpec tbSpec = tbSpecService.findById(id);
        return YouthR.ok().data("tbSpec",tbSpec);
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/specQueryByIdList/{id}")
    public YouthR specFindById(@PathVariable Integer id) {
        List<TbSpec> tbSpecs = tbSpecService.queryByI(id);
        return YouthR.ok().data("tbSpecs",tbSpecs);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbSpec>  list = tbSpecService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbSpec> pageUtils = tbSpecService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbSpec tbSpec){
        PageUtil<TbSpec> pageUtils = tbSpecService.pageQueryByAll(pageNum, pageSize, tbSpec);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/addSpec")
    public YouthR addSpec(@RequestBody TbSpec tbSpec) {

        boolean b = tbSpecService.saveTbSpec(tbSpec);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbSpec tbSpec) {
        boolean b = tbSpecService.updateTbSpec(tbSpec);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbSpecService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Spec> findAll(){
        return specService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Spec> findPage(int page, int size){
        return specService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Spec> findList(@RequestBody Map<String,Object> searchMap){
        return specService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Spec> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  specService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Spec findById(Integer id){
        return specService.findById(id);
    }



}
